<template>
	<view>
		<cu-custom :bgColor="globalData.bgColor" :isBack="true">
			<template #content>
				<text class="text-darkgrey">{{ $t('points.pointsconfig.06g7ik') }}</text>
			</template>
		</cu-custom>
		<view>
			<form>
				<view class="cu-form-group solid-top">
					<view class="title text-darkgrey">
						<text class="text-red">*</text>
						{{ $t('points.pointsconfig.oa9g78') }}
					</view>
					<input class="text-right" :placeholder="$t('points.pointsconfig.oa9g78')" name="initPosts" type="number" v-model="form.initPosts" />
				</view>
				<view class="cu-form-group text-darkgrey">
					<view class="title">
						<text class="text-red">*</text>
						{{ $t('coupon.couponuser.3261ox') }}
					</view>
					<input class="text-right" :placeholder="$t('points.pointsconfig.pjfyjm')" name="initVipPosts" type="number" v-model="form.premiseAmount" />
				</view>
				<view class="cu-form-group margin-top-xs text-darkgrey">
					<view class="title">
						<text class="text-red">*</text>
						{{ $t('points.pointsconfig.4f1i8o') }}
					</view>
					<input
						class="text-right"
						:placeholder="$t('points.pointsconfig.ceh5uf')"
						name="defaultDeductScale"
						type="number"
						min="0"
						max="100"
						v-model="form.defaultDeductScale"
					/>
				</view>
				<view class="text-sm text-purple-grey padding-top-xs padding-bottom-xs padding-left padding-right">
					<text class="cuIcon-info"></text>
					{{ $t('points.pointsconfig.5y72r1') }}
				</view>

				<view class="cu-form-group text-darkgrey">
					<view class="title">
						<text class="text-red">*</text>
						{{ $t('points.pointsconfig.5r6q25') }}
					</view>
					<input class="text-right" :placeholder="$t('points.pointsconfig.673j18')" name="defaultDeductAmount" type="number" v-model="form.defaultDeductAmount" />
				</view>
				<view class="text-sm text-purple-grey padding-top-xs padding-bottom-xs padding-left padding-right">
					<text class="cuIcon-info"></text>
					{{ $t('points.pointsconfig.5v11jk') }}
				</view>
			</form>

			<view class="padding-xl flex flex-direction" v-if="globalData.permissions['mall:pointsconfig:edit']">
				<button class="cu-btn bg-blue shadow-blur round lg" @tap="submit">{{ $t('order.logisticsform.tc3h47') }}</button>
			</view>
		</view>
	</view>
</template>

<script>
import { getPage, getObj, addObj, putObj, delObj, getObj2 } from '@/api/mall/pointsconfig';

export default {
	onLoad(e) {
		this.getData();
	},
	data() {
		return {
			globalData: getApp().globalData,
			CustomBar: this.CustomBar,
			id: null,
			form: {
				defaultDeductAmount: 0,
				defaultDeductScale: 0,
				initPosts: 0,
				initVipPosts: 0,
				premiseAmount: 0
			},
			index: 0
		};
	},
	methods: {
		submit() {
			if (this.validate()) {
				if (this.form.defaultDeductScale > 100) {
					uni.showToast({
						icon: 'none',
						title: this.$t('points.pointsconfig.a1u7t1')
					});
					return;
				}
				putObj(this.form).then((response) => {
					if (response.data) {
						uni.showToast({ title: this.$t('order.logisticsform.34793l') });
						uni.navigateBack({});
					}
				});
			}
		},
		validate() {
			//验证
			let validate = true;
			let names = [
				{ key: 'defaultDeductAmount', label: this.$t('points.pointsconfig.673j18') },
				{ key: 'defaultDeductScale', label: this.$t('points.pointsconfig.ceh5uf') },
				{ key: 'premiseAmount', label: this.$t('points.pointsconfig.3f1u5a') },
				{ key: 'initVipPosts', label: this.$t('points.pointsconfig.x42hzj') },
				{ key: 'initPosts', label: this.$t('points.pointsconfig.oa9g78') }
			];
			names.map((item) => {
				if (!(this.form[item.key] && this.form[item.key] >= 0)) {
					validate = false;
					uni.showToast({
						icon: 'none',
						title: item.label + this.$t('points.pointsconfig.linbsc')
					});
					return;
				}
			});
			return validate;
		},
		getData() {
			getObj2().then((response) => {
				this.form = response.data ? response.data : {};
			});
		}
	}
};
</script>

<style>
.cu-form-group .title {
	min-width: calc(4em + 15px);
}
</style>
